package com.relytosoft.mizar.analyze.strategy.algorithm.face.service.impl;

import cn.hutool.json.JSONUtil;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.base.constant.MediaCommonConstants;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.*;
import com.relytosoft.mizar.analyze.common.config.properties.MizarAnalyzeProperties;
import com.relytosoft.mizar.analyze.strategy.algorithm.face.feign.AlgoFaceFeignService;
import com.relytosoft.mizar.analyze.strategy.algorithm.face.service.FaceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jierry
 * @version 2022/5/11.
 */
@Service
public class FaceServiceImpl implements FaceService {

    @Resource
    private AlgoFaceFeignService algoFaceFeignService;

    @Resource
    private MizarAnalyzeProperties mizarAnalyzeProperties;

    @Override
    public ResponseResult test() {
        try {
            Map<String, Object> bodys = new HashMap();
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.createTask(new URI(strUrl), JSONUtil.toJsonStr(bodys));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult createTask(TaskRequestDto taskRequestDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            String strUploadUrl = String.format("%1$s://%2$s:%3$s%4$s%5$s?%6$s=%7$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, MediaCommonConstants.ALGORITHM_RECEVICE_METHOD_NAME, "key", mizarAnalyzeProperties.getKey());
            String strStatusUploadUrl = String.format("%1$s://%2$s:%3$s%4$s%5$s?%6$s=%7$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, MediaCommonConstants.ALGORITHM_STATUS_METHOD_NAME, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.createTask(new URI(strUrl), JSONUtil.toJsonStr(taskRequestDto), strStatusUploadUrl, strUploadUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult stopTask(String taskId) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.stopTask(new URI(strUrl), taskId);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult feartureExtraction(FeatureExtractionReqDto featureExtractionReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.feartureExtraction(new URI(strUrl), JSONUtil.toJsonStr(featureExtractionReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult feartureComparison(FeatureComparisonReqDto featureComparisonReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.feartureComparison(new URI(strUrl), JSONUtil.toJsonStr(featureComparisonReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult createGroup(FaceGroupReqDto faceGroupReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.createGroup(new URI(strUrl), JSONUtil.toJsonStr(faceGroupReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult deleteGroup(FaceGroupReqDto faceGroupReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.deleteGroup(new URI(strUrl), JSONUtil.toJsonStr(faceGroupReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult createPerson(FacePersonReqDto facePersonReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.createPerson(new URI(strUrl), JSONUtil.toJsonStr(facePersonReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult deletePerson(FacePersonReqDto facePersonReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.deletePerson(new URI(strUrl), JSONUtil.toJsonStr(facePersonReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult searchPerson(FaceSearchReqDto faceSearchReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.searchPerson(new URI(strUrl), JSONUtil.toJsonStr(faceSearchReqDto));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult createDispostion(DispositionReqDto dispositionReqDto) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            String strUploadUrl = String.format("%1$s://%2$s:%3$s%4$s%5$s?%6$s=%7$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, MediaCommonConstants.ALGORITHM_FACE_ALARM_METHOD_NAME, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.createDisposition(new URI(strUrl), JSONUtil.toJsonStr(dispositionReqDto), strUploadUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult deleteDispostion(String dispositionId) {
        try {
            String strUrl = String.format("%1$s://%2$s:%3$s%4$s?%5$s=%6$s", mizarAnalyzeProperties.getProtocol(), mizarAnalyzeProperties.getGatewayIP(), mizarAnalyzeProperties.getGatewayPort(), MediaCommonConstants.ALGORITHM_FACE_BITMAIN_SERVICE_URL, "key", mizarAnalyzeProperties.getKey());
            return algoFaceFeignService.deleteDisposition(new URI(strUrl), dispositionId);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}
